<?php

namespace App\Admin\Forms\User;

use App\Models\WxTenant;
use App\Models\WxUniversity;
use App\Wen\Utils\Settings;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;

class UserStudentCertifyForm extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        $tenants = WxTenant::all();
        if($tenants){
            foreach ($tenants as $tenant){
                if($tenant->tenant_id > 0){
                    Settings::set('tenant_'.$tenant->tenant_id.'_with_student_certify', $input['tenant_'.$tenant->tenant_id.'_with_student_certify'] ?? []);
                }
            }
        }
        return $this->response()
            ->success('操作成功')
            ->refresh();
    }

    public function form()
    {
        $this->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;display: inline-block">这是配置哪个分站可以审核哪些学校的学生认证</div>';
        }, '');

        $tenants = WxTenant::all();
        if($tenants){
            foreach ($tenants as $tenant){
                if($tenant->tenant_id > 0){
                    $this->multipleSelect('tenant_'.$tenant->tenant_id.'_with_student_certify', $tenant->name)->options(function ($sids) {
                        if($sids){
                            $res = [];
                            foreach ($sids as $sid){
                                $university = WxUniversity::where('sid', $sid)->first();
                                if ($university) {
                                    $res[$university->sid] = $university->name;
                                }
                            }
                            return $res;
                        }
                    })->placeholder('请输入学校名字进行搜索')->ajax('select/universities')->default(Settings::get('tenant_'.$tenant->tenant_id.'_with_student_certify', []));
                }
            }
        }
    }
}
